<?PHP
require_once WACT_ROOT . 'controller/controller.inc.php';
require_once APP_ROOT . "view/common/layout.formview.php";
require_once APP_ROOT . "view/common/layout.view.php";
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT .'model/common/sportmail.php';
require_once APP_ROOT . "model/common/site.php";

class PswdreminderView extends LayoutFormView {

    function PswdreminderView($TemplateFile) {
        parent :: LayoutFormView($TemplateFile);

    }

    function prepare(&$source, &$request, &$responseModel){
    	parent::prepare();

    	if(APP_NAME!="") {
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Password reminder!');      

 
    	if($responseModel->get('invalid')=="true"){
    			$this->Template->set('nouser','You entered wrong username or email. Please, try again.');
    	}
    }

}

class PswdreminderPage extends PageController {
	function 	PswdreminderPage(){
		parent::PageController();

		$Form =& new FormController();

		$Form->addChild('submit', new ButtonController(new Delegate($this, 'submit')));
		$Form->setDefaultChild('submit');

		$Form->addRule(new RequiredRule('login'));
		$Form->addRule(new RequiredRule('email'));
		$Form->addRule(new PatternRule('email','/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'));

		$this->addChild("pswdreminder_form",$Form);
		$this->setDefaultChild('pswdreminder_form');


        $mytemplate = 'pub/pswdreminder.html';


		$this->setDefaultView(new PswdreminderView ($mytemplate));
	}

	function submit(&$source, &$request, &$responseModel){
		if($responseModel->isValid()){
			$login=$responseModel->get('login');
			$email=$responseModel->get('email');
			$user=User::getRecordbyLogin($login);
			if($user!=null){
				$realemail=$user->get('email');
				if($realemail==$email){
					$password=$user->get('password');
					Sportmail::sendPswdreminderMail($email,$login,$password);

					$redirect_url='http://'.$_SERVER['HTTP_HOST'].'/pswdsent.htm';
    	 			if(defined('SID')&&(!isset($_COOKIE[session_name()])))
    	 			$redirect_url.='?'.SID;
    	 			header('Location: '.$redirect_url);

					return 1;
				}
			}
			$responseModel->set('invalid','true');
		}
	}

}

class PswdsentView extends LayoutView{

    function PswdsentView($TemplateFile) {
        parent :: View($TemplateFile);
    }

    function prepare() {
    	parent::prepare();

        $this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Password reminder');      

 
    

    }
}

class PswdsentPage extends PageController {

    function PswdsentPage() {
        parent::PageController();

        $registerpage="http://".$_SERVER['HTTP_HOST'].'/pswdreminder.htm';
        $from=getenv("HTTP_REFERER");


        $mytemplate = 'pub/pswdsent.html';


        $mytemplate2 = 'pub/notfound.html';


        if($from!=null) {
        	$arrayfrom=explode('?',$from);
        	$from=$arrayfrom[0];
        	if($from==$registerpage)
        		$this->setDefaultView(new PswdsentView($mytemplate));
        }	else
        	$this->setDefaultView(new NotFoundView($mytemplate2));
    }

}

?>
